package panel.mapPanel
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import baidu.map.basetype.LngLat;
	import baidu.map.basetype.Size;
	import baidu.map.config.Anchor;
	import baidu.map.control.base.Ruler;
	import baidu.map.control.base.Scaler;
	import baidu.map.core.Map;
	import baidu.map.layer.Layer;
	import baidu.map.layer.RasterLayer;
	import baidu.map.overlay.Label;
	import baidu.map.overlay.Marker;
	import baidu.map.overlay.geometry.Polyline;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.TransformGestureEvent;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import main.ClientConstants;
	
	public class BaiduMap extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var map:Map;
		private var layer:Layer;
		private var scaler:Scaler;
		private var ruler:Ruler;
		
		private var _wid:Number;
		private var _hei:Number;
		
		private var markerList:Array;
		
		public function BaiduMap(stage:Stage, loader:ResourceLoader, lang:Lang,uri:URI = null)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			_wid = 0;
			_hei = 0;
			
			markerList = [];
			
		}
		
		public function init(wid:Number,hei:Number):void
		{
			_wid = wid;
			_hei = hei;
			
			map = new Map(new Size(wid,hei));
			addChild(map);
			// 初始化Map的中心点和显示级别
			map.centerAndZoom(new LngLat(116.404, 39.915), 12);
			// 添加底图
			layer = new RasterLayer("BaiduMap", map);
			map.addLayer(layer);
			
			map.defaultCursor.visible = false;
			map.draggingCursor.visible = false;
			
			// 添加Scaler			
			scaler = new Scaler(map);
			map.addControl(scaler);
			scaler.anchor = Anchor.TL;
			scaler.offset = new Size(_stage.stageWidth * .01,_stage.stageHeight * .01);
			scaler.scaleY = 1.5;
			scaler.scaleX = 1.2;
			
			// 添加Ruler			
			ruler = new Ruler(map);
			map.addControl(ruler);
			
			if(Multitouch.supportsGestureEvents){
				Multitouch.inputMode = MultitouchInputMode.GESTURE;
				map.addEventListener(TransformGestureEvent.GESTURE_ZOOM,zoomHandler);
			}
		}
		
		public override function get width():Number
		{
			return _wid;
		}
		
		public override function get height():Number
		{
			return _hei;
		}
		
		/**
		 * 
		 * @param lon 经度
		 * @param lat 纬度
		 * 
		 */		
		public function moveToCenter(lon:Number,lat:Number):void
		{
			map.center = new LngLat(lat,lon);//(lon,lat);
		}
		/**
		 * 
		 * @param scale  缩放比例
		 * 
		 */		
		public function changeZom(scale:int):void
		{
			map.zoom = scale;
		}
		
		protected function zoomHandler(e:TransformGestureEvent):void
		{
			if(e.scaleX > 1){
				if(e.scaleX > 1.1){
					map.zoom += 1;
				}
			}else{
				if(e.scaleX < .9){
					map.zoom -= 1;
				}
			}
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				map.removeEventListener(TransformGestureEvent.GESTURE_ZOOM,zoomHandler);
				
				ruler.dispose();
				scaler.dispose();
				layer.dispose();
				//				map.dispose();
				ruler = null;
				scaler = null;
				layer = null;
				map = null;
				
				System.gc();
			}
		}
		
		public function addLabel(lon:Number,lat:Number,type:int,str:String):void//list:Array
		{
			var markerIcon:Bitmap;
			
			if(type == ClientConstants.VISIT_TYPE_SCENIC){
				markerIcon = new _loader.uiManager.mapMarkRed as Bitmap;
			}if(type == ClientConstants.VISIT_TYPE_EATING){
				markerIcon = new _loader.uiManager.mapMarkYellow as Bitmap;
			}if(type == ClientConstants.VISIT_TYPE_FUN){
				markerIcon = new _loader.uiManager.mapMarkBlue as Bitmap;
			}if(type == ClientConstants.VISIT_TYPE_HOTEL){
				markerIcon = new _loader.uiManager.mapMarkBlue as Bitmap;
			}
			
			
			markerIcon.width = _stage.stageWidth * .08;
			markerIcon.height = markerIcon.width;
			
			var marker:Marker = new Marker();
			marker.position = new LngLat(lat,lon);
			
//			marker.offset = new Size(-_stage.stageWidth * .04,-_stage.stageWidth * .08);
			marker.icon = markerIcon;
			marker.content = str;
			
			map.addOverlay(marker);
			
			markerList.push(marker);
		}
		/**
		 * 
		 * @param pointList  坐标点列表
		 */		
		public function addLine(pointList:Array):void
		{
			var coords:Vector.<LngLat> = new Vector.<LngLat>();
			for(var i:int = 0;i < pointList.length;i++){
				coords.push(new LngLat(pointList[i][1], pointList[i][0]));
			}
			// 创建折线覆盖物
			var polyline:Polyline = new Polyline(coords);
			map.addOverlay(polyline);
		}
	}
}